﻿
#pragma once

#if FUEL_DEVICE_GLES2

#define GL_GLEXT_PROTOTYPES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#if FUEL_OS_WINDOWS
#pragma comment(lib, "libEGL.lib")
#pragma comment(lib, "libGLESv2.lib")
#elif FUEL_OS_ANDROID
#elif FUEL_OS_IOS
#endif

// 贴图槽的绑定名，8层，分别是0-7，即 IN_TEX0 -> IN_TEX7
#define FUEL_GLES2_TEX _FTA("IN_TEX")

FINLINE bool FGetGLES2Format(EF_FORMAT fmt, GLint& internalformat, GLenum& format, GLenum& type)
{
    switch (fmt)
    {
    case eFMT_A8R8G8B8:
        //internalformat = GL_BGRA_EXT; format = GL_BGRA_EXT; type = GL_UNSIGNED_BYTE;
        internalformat = GL_RGBA; format = GL_RGBA; type = GL_UNSIGNED_BYTE; // 统一在SHADER中翻转R和B颜色
        return true;
    case eFMT_X8R8G8B8:
        //internalformat = GL_BGRA_EXT; format = GL_BGRA_EXT; type = GL_UNSIGNED_BYTE;
        internalformat = GL_RGBA; format = GL_RGBA; type = GL_UNSIGNED_BYTE; // 统一在SHADER中翻转R和B颜色
        return true;
    case eFMT_A8B8G8R8:
        internalformat = GL_RGBA; format = GL_RGBA; type = GL_UNSIGNED_BYTE;
        return true;
    case eFMT_X8B8G8R8:
        internalformat = GL_RGBA; format = GL_RGBA; type = GL_UNSIGNED_BYTE;
        return true;
    case eFMT_A16B16G16R16:
        internalformat = GL_RGBA; format = GL_RGBA; type = GL_SHORT;
        return true;
    case eFMT_A16B16G16R16F:
        internalformat = GL_RGBA; format = GL_RGBA; type = GL_HALF_FLOAT_OES;
        return true;
    case eFMT_A32B32G32R32F:
        internalformat = GL_RGBA; format = GL_RGBA; type = GL_FLOAT;
        return true;

    case eFMT_D16:
        internalformat = GL_LUMINANCE; format = GL_LUMINANCE; type = GL_HALF_FLOAT_OES;
        return true;
    case eFMT_D32:
        internalformat = GL_LUMINANCE; format = GL_LUMINANCE; type = GL_HALF_FLOAT_OES;
        return true;

    case eFMT_A8L8:
        internalformat = GL_LUMINANCE_ALPHA; format = GL_LUMINANCE_ALPHA; type = GL_UNSIGNED_BYTE;
        return true;
    case eFMT_V8U8:
        internalformat = GL_LUMINANCE_ALPHA; format = GL_LUMINANCE_ALPHA; type = GL_UNSIGNED_BYTE;
        return true;
    }
    return false;
}

FINLINE GLenum FGetGLES2Primitive(EF_PRIMITIVE_TYPE type)
{
    switch (type)
    {
    case ePVT_POINTLIST:     return GL_POINTS;
    case ePVT_LINELIST:      return GL_LINES;
    case ePVT_LINESTRIP:     return GL_LINE_STRIP;
    case ePVT_TRIANGLELIST:  return GL_TRIANGLES;
    case ePVT_TRIANGLESTRIP: return GL_TRIANGLE_STRIP;
    case ePVT_TRIANGLEFAN:   return GL_TRIANGLE_FAN;
    }
    return GL_TRIANGLES;
}

FINLINE const ACHAR* FGetGLES2StreamName(EF_STREAM_TYPE t)
{
    switch (t)
    {
    case eST_POSITION:      return _FTA("IN_POS");
    case eST_NORMAL:        return _FTA("IN_N");
    case eST_COLOR:         return _FTA("IN_COLOR");
    case eST_UV:            return _FTA("IN_UV");
    case eST_TANGENT:       return _FTA("IN_T");
    case eST_BLENDINX:      return _FTA("IN_BI");
    case eST_BLENDWEIGHT2:  return _FTA("IN_BW2");
    case eST_BLENDWEIGHT4:  return _FTA("IN_BW4");
    }
    return _FNILA;
}

#endif
